package example.com.zcq.mymj.fragment;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import example.com.zcq.mymj.Adapter.TabPageAdapter;
import example.com.zcq.mymj.R;

/**
 * @author zcq
 * @package example.com.zcq.mymj.fragment
 * @fileName MyMj
 * @description: 这个类的描述
 * @org 融信通达科技有限公司
 * @email @163.com
 * @date 2018/11/22  19:59
 */
public class Fragment6 extends Fragment {


    private View view;
    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private List<Fragment> fragments;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fg6, container, false);


        initView(view);
        return view;
    }


    private void initView(View view) {
        mTabLayout = (TabLayout) view.findViewById(R.id.tabLayout);
        mViewPager = (ViewPager) view.findViewById(R.id.viewPager);
        //使用Tablayout的newTab()创建tab
//        TabLayout.Tab tab1 = mTabLayout.newTab().setText("诗词");
//        TabLayout.Tab tab2 = mTabLayout.newTab().setText("古文");
//        TabLayout.Tab tab3 = mTabLayout.newTab().setText("文章");
//
//        mTabLayout.addTab(tab1);
//        mTabLayout.addTab(tab2);
//        mTabLayout.addTab(tab3);

        fragments=new ArrayList<>();
        //将提前写好三个Fragment添加到集合中
        fragments.add(new FragmentTab1());
        fragments.add(new FragmentTab2());
        fragments.add(new FragmentTab3());
        fragments.add(new FragmentTab4());
        fragments.add(new FragmentTab5());
        fragments.add(new FragmentTab6());
        String[] mTabNames=new String[]{"四大名著","白话古籍","古文鉴赏","西游专题","历史演绎","三言两拍"};
        //创建适配器
        TabPageAdapter pageAdapter = new TabPageAdapter(getActivity().getSupportFragmentManager(), fragments,mTabNames);
        //设置ViewPager的适配器
        mViewPager.setAdapter(pageAdapter);

        mTabLayout.setupWithViewPager(mViewPager);


        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                //tab项选中状态时执行
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                //tab项取消选中状态时执行

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                //tab项选中状态再次点击时执行

            }
        });




    }
}
